home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / SAM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-23  |  6.5 KB  |  237 lines

  1. #ifdef MSDOS
  2. #include "global.h"
  3. #ifdef SAMCALLB
  4. #include "commands.h"
  5. #include "mbuf.h"
  6. #include "socket.h"
  7. #include "session.h"
  8. #include "netuser.h"
  9. #include "proc.h"
  10. #include "tty.h"
  11. #include "samapi.h"            /* samapi interface spec */
  12.  
  13. #if !defined(_lint)
  14. static char rcsid[] OPTIONAL = "$Id: sam.c,v 1.10 1996/12/23 20:37:36 root Exp root $";
  15. #endif
  16.  
  17. /*
  18.  * functions in samlib.c
  19.  */
  20.  
  21. int LocateSam(void);
  22. int CallSam(int cmd, void *cmdbuf, void *rspbuf);
  23. extern char *Callserver;  /* buckbook.c */
  24. extern int usesplit;
  25. int cb_lookup (int s,char *str,FILE *fp);
  26. static char Nofind[] = "*** Call not found in SAM database of %s as of %s\n\n";
  27. static char callhdr[] = "Amateur Radio Callsign: %s  (%s Class)  born in 19%s\n";
  28. int SAMoutbytes = 0;
  29. extern void *mallocw (unsigned nb);
  30. extern void leadingCaps (char *str, int mode);
  31.  
  32.  
  33. int
  34. docallbook(argc,argv,p)
  35. int argc;
  36. char *argv[];
  37. void *p;
  38. {
  39. struct sockaddr_in sock;
  40. const char *cp;
  41. int s,i;
  42. struct mbuf *bp;
  43. struct session *sp;
  44. int thesocket, err;
  45.  
  46.     /*Make sure this comes from console - WG7J*/
  47.     if(Curproc->input != Command->input)
  48.         return 0;
  49.  
  50.     /* Allocate a session descriptor */
  51.     if((sp = newsession(argv[1],TELNET,0)) == NULLSESSION){
  52.         tputs(TooManySessions);
  53.         keywait(NULLCHAR,1);
  54.         return 1;
  55.     }
  56.     sp->ttystate.echo = sp->ttystate.edit = 0;
  57.     sp->flowmode = 1;
  58.     sock.sin_family = AF_INET;
  59.     sock.sin_port = IPPORT_CALLDB;
  60.     thesocket = Curproc->output;
  61.     for(i=1;i<argc;i++){
  62.         if (!Callserver || !*Callserver)    {
  63.             tputc('\n');
  64.             if ((err = cb_lookup (Curproc->output, argv[i], (FILE *) 0)) != 0)
  65.                 tprintf ((err == 1) ? "SAMAPI not loaded!\n" : "Amateur Call '%s' not found or invalid!\n", argv[i]);
  66.             continue;
  67.         }
  68.         tprintf("Resolving %s... ",Callserver);
  69.         if((sock.sin_addr.s_addr = resolve(Callserver)) == 0){
  70.             tprintf("Host %s unknown\n",Callserver);
  71.             continue;
  72.         }
  73.         tprintf("trying %s",psocket((struct sockaddr *)&sock));
  74.         if((sp->s = s = socket(AF_INET,SOCK_STREAM,0)) == -1){
  75.             tputs(Nosock);
  76.         break;
  77.         }
  78.         sockmode(s,SOCK_ASCII);
  79.         if(connect(s,(char *)&sock,sizeof(sock)) == -1){
  80.             cp = sockerr(s);
  81.             tprintf(" -- Connect failed: %s\n",cp != NULLCHAR ? cp : "");
  82.             close_s(s);
  83.             sp->s = -1;
  84.             continue;
  85.         }
  86.         tputc('\n');
  87.         usflush(thesocket);
  88.         usprintf(s,"%s\n",argv[i]);
  89.         while(recv_mbuf(s,&bp,0,NULLCHAR,(int *)0) > 0){
  90.             send_mbuf(thesocket,bp,0,NULLCHAR,0);
  91.         }
  92.         close_s(s);
  93.         sp->s = -1;
  94.     }
  95.     keywait(NULLCHAR,1);
  96.     freesession(sp);
  97.     return 0;
  98. }
  99.  
  100.  
  101. static int
  102. cb_look (str, incall, outcall, err)
  103. char *str;
  104. cmdfindcall_t *incall;
  105. rspdatarec_t *outcall;
  106. int *err;
  107. {
  108.     if (!str || strlen(str) > 6)
  109.         return 2;        /* not an amateur call */
  110.     /* make sure the resident code (SAMAPI.EXE) has been installed */
  111.     if (LocateSam())
  112.         return 1;
  113.  
  114.     /* build command block and call SAMAPI, function SamFindCall */
  115.     incall->packflags = 0;    /* 0 to unpack all data record fields */
  116.     strncpy(incall->call, str, 6);
  117.     incall->call[6] = 0;
  118.     *err = CallSam(SamFindCall, incall, outcall);
  119.     return 0;
  120. }
  121.  
  122.  
  123. char *
  124. cb_lookname (str)
  125. char *str;
  126. {
  127. int err;
  128. cmdfindcall_t sam_in;    /* buffer for samapi find command */
  129. rspdatarec_t sam_out;    /* buffer for result of samapi find command */
  130. char *name, from[8], *cp;
  131.  
  132.     strncpy (from, str, 6);
  133.     from[6] = 0;
  134.     if ((cp = strchr (from, '@')) != 0)
  135.         *cp = 0;
  136.     if ((cp = strchr (from, '%')) != 0)
  137.         *cp = 0;
  138.  
  139.     if (cb_look (from, &sam_in, &sam_out, &err) || err == SerrNotFound)
  140.         return ((char *) 0);
  141.      
  142.     name = mallocw(strlen(sam_out.d.FirstName) + strlen(sam_out.d.LastName) + 7);
  143.     if (name)    {
  144.         leadingCaps (&sam_out.d.FirstName[1], 1);
  145.         leadingCaps (&sam_out.d.LastName[1], 0);
  146.         sprintf(name, " (%s ", sam_out.d.FirstName);
  147.         if (sam_out.d.MidInitial[0] != ' ')
  148.             sprintf (&name[strlen(name)], "%s. ", sam_out.d.MidInitial);
  149.         sprintf (&name[strlen(name)], "%s)", sam_out.d.LastName);
  150.     }
  151.     return (name);
  152. }
  153.  
  154.  
  155.  
  156. /* return values - 1=SAMAPI not found, 2=call not found or invalid, 0=okay */
  157. int
  158. cb_lookup (s, str, fp)
  159. int s;
  160. char *str;
  161. FILE *fp;
  162. {
  163. int err, response;
  164. cmdfindcall_t sam_in;    /* buffer for samapi find command */
  165. rspdatarec_t sam_out;    /* buffer for result of samapi find command */
  166. cmdfindcounty_t cty_in;    /* buffer for find county command */
  167. rspfindcounty_t cty_out;    /* buffer for find county response */
  168. rhdr_t date_in;
  169. rspdbdate_t date_out;
  170. char *class;
  171.  
  172.     SAMoutbytes = 0;
  173.     if ((response = cb_look (str, &sam_in, &sam_out, &err)) != 0)
  174.         return response;
  175.  
  176.     /* check for unusual error something other that plain ole not found */
  177.     if (err != 0 && err != SerrNotFound)
  178.         return 2;
  179.  
  180.     /* check for just not found */
  181.  
  182.     if (err == SerrNotFound)    {
  183.         err = CallSam(SamGetDatabaseDate, &date_in, &date_out);
  184.         if (fp)
  185.             SAMoutbytes += fprintf (fp, Nofind, date_out.scope, date_out.date);
  186.         else
  187.             SAMoutbytes += usprintf(s, Nofind, date_out.scope, date_out.date);
  188.         return 2;
  189.     }
  190.  
  191.     switch (sam_out.d.Class[0])    {
  192.         case 'N':    class = "Novice";
  193.                 break;
  194.         case 'T':    class = "Technician";
  195.                 break;
  196.         case 'G':    class = "General";
  197.                 break;
  198.         case 'A':    class = "Advanced";
  199.                 break;
  200.         case 'E':    class = "Extra";
  201.                 break;
  202.     }
  203.  
  204.     if (fp)    {
  205.         /* display call with leading space stripped */
  206.         SAMoutbytes += fprintf(fp, callhdr, sam_out.d.Call + (sam_out.d.Call[0] == ' '), class, sam_out.d.Dob);
  207.  
  208.         /* display first m last, but leave out middle and space if no middle initial */
  209.         SAMoutbytes += fprintf(fp, "%s ", sam_out.d.FirstName);
  210.         if (sam_out.d.MidInitial[0] != ' ')
  211.             SAMoutbytes += fprintf(fp, "%s ", sam_out.d.MidInitial);
  212.         SAMoutbytes += fprintf(fp, "%s\n", sam_out.d.LastName);
  213.  
  214.         /* address line, then city, st zip */
  215.         SAMoutbytes += fprintf(fp, "%s\n", sam_out.d.Address);
  216.         SAMoutbytes += fprintf(fp, "%s, %s %s\n\n", sam_out.d.City, sam_out.d.State, sam_out.d.Zip);
  217.     } else    {
  218.         /* display call with leading space stripped */
  219.         SAMoutbytes += usprintf(s, callhdr, sam_out.d.Call + (sam_out.d.Call[0] == ' '), class, sam_out.d.Dob);
  220.  
  221.         /* display first m last, but leave out middle and space if no middle initial */
  222.         SAMoutbytes += usprintf(s, "%s ", sam_out.d.FirstName);
  223.         if (sam_out.d.MidInitial[0] != ' ')
  224.             SAMoutbytes += usprintf(s, "%s ", sam_out.d.MidInitial);
  225.         SAMoutbytes += usprintf(s, "%s\n", sam_out.d.LastName);
  226.  
  227.         /* address line, then city, st zip */
  228.         SAMoutbytes += usprintf(s, "%s\n", sam_out.d.Address);
  229.         SAMoutbytes += usprintf(s, "%s, %s %s\n\n", sam_out.d.City, sam_out.d.State, sam_out.d.Zip);
  230.     }
  231.     return 0;
  232. }
  233. #endif
  234.  
  235.  
  236. #endif    /* MSDOS */
  237.